package com.lingjtx.ai.controller;

import com.lingjtx.ai.po.AiChatQueryPo;
import com.lingjtx.ai.po.ChatMsgFeedbackPo;
import com.lingjtx.ai.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * AI 对话
 */
@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    private ChatService chatService;

    /**
     * 发送对话消息
     */
    @PostMapping(value = "/completions", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ServerSentEvent<String>> completions(@RequestBody AiChatQueryPo model, ServerHttpRequest request) {
        return chatService.completions(model, request);
    }


    /**
     * 停止响应
     */
    @PostMapping("/stop_stream/{task_id}")
    public Mono<String> stop(@PathVariable("task_id") String task_id,
                             @RequestBody ChatMsgFeedbackPo model) {
        return chatService.stop(task_id, model);
    }

    /**
     * 消息反馈（点赞）
     */
    @PostMapping("/feedback/{message_id}")
    public Mono<String> feedback(@PathVariable("message_id") String message_id,
                                 @RequestBody ChatMsgFeedbackPo model) {
        return chatService.feedback(message_id, model);
    }
}
